package com.yifeng.repo.base.service.impl;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yifeng.base.mcs.api.EventNotifyRestService;
import com.yifeng.base.mcs.api.dto.EventTypeOptionalDto;
import com.yifeng.base.mcs.api.dto.SimpleEventNotifyDto;
import com.yifeng.base.mcs.api.params.EventNotifyCreateParam;
import com.yifeng.repo.base.params.ConsoleParam;
import com.yifeng.repo.base.security.proxy.CommonProxy;
import com.yifeng.repo.base.utils.codec.URLEncodeHelper;
import com.yifeng.repo.base.utils.converter.DateHelper;
import com.yifeng.repo.base.utils.converter.JacksonHelper;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by daibing on 2024/2/3.
 */
public class EventNotifyRestServiceImpl extends CommonProxy implements EventNotifyRestService {
    private final String mapping;

    public EventNotifyRestServiceImpl(ConsoleParam consoleParam) {
        super(consoleParam);
        this.mapping = "/yfcloud-mcs/rest/EventNotifyRestService";
    }

    @Override
    public List<EventTypeOptionalDto> listOptional(String appId, String eventType) {
        String url = consoleParam.getUrl() + mapping + "/listOptional";
        Map<String, String> params = new HashMap<>(2);
        params.put("appId", appId);
        params.put("eventType", eventType);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, EventTypeOptionalDto.class);
    }

    @Override
    public Long create(EventNotifyCreateParam param) {
        String url = consoleParam.getUrl() + mapping + "/create";
        String body = JacksonHelper.toJson(param);
        return this.doActionCallByRpc(url, body, Long.class);
    }

    @Override
    public boolean confirm(String appId, String eventType, Long eventId, String storeCode, String confirmBy) {
        String url = consoleParam.getUrl() + mapping + "/confirm";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("appId", appId);
        params.put("eventType", eventType);
        params.put("eventId", eventId.toString());
        params.put("storeCode", storeCode);
        params.put("confirmBy", confirmBy);
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public boolean cancel(String appId, String eventType, Long eventId, String confirmBy, LocalDateTime startTime, LocalDateTime endTime) {
        String url = consoleParam.getUrl() + mapping + "/cancel";
        ObjectNode params = JacksonHelper.createEmptyJsonNode();
        params.put("appId", appId);
        params.put("eventType", eventType);
        params.put("eventId", eventId.toString());
        params.put("confirmBy", confirmBy);
        params.put("startTime", URLEncodeHelper.encode(DateHelper.getDateTime(startTime)));
        params.put("endTime", URLEncodeHelper.encode(DateHelper.getDateTime(endTime)));
        return this.doActionCallByRpc(url, params.toString(), Boolean.class);
    }

    @Override
    public List<SimpleEventNotifyDto> listNotify(String appId, String eventType, String storeCode) {
        String url = consoleParam.getUrl() + mapping + "/listNotify";
        Map<String, String> params = new HashMap<>(3);
        params.put("appId", appId);
        params.put("eventType", eventType);
        params.put("storeCode", storeCode);
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, SimpleEventNotifyDto.class);
    }

    @Override
    public List<SimpleEventNotifyDto> listVoice(String appId, String eventType, String storeCode, Long lastTimestamp) {
        String url = consoleParam.getUrl() + mapping + "/listVoice";
        Map<String, String> params = new HashMap<>(4);
        params.put("appId", appId);
        params.put("eventType", eventType);
        params.put("storeCode", storeCode);
        params.put("lastTimestamp", String.valueOf(lastTimestamp));
        String queryParameters = buildQueryParameters(params);
        String requestUrl = String.format("%s?%s", url, queryParameters);
        return this.doListCallByRpc(requestUrl, SimpleEventNotifyDto.class);
    }

}
